package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查项管理
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    /**
     *
     * @param checkItem
     * @return
     * 前台传过来的是json字符串，所以需要@RequestBody进行数据类型转换，并映射到CheckItem中
     * Result公共返回结果集
     */
    //新增检查项
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            //添加成功
            checkItemService.add(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //执行出现异常表示操作失败，返回失败信息
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        //返回操作成功信息
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    //分页查询结果
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = checkItemService.pageQuery(queryPageBean);
        return pageResult;
    }

    //删除操作
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result delete(Integer id){
        try {
            //删除成功
            checkItemService.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            //service层抛出的异常，包含异常信息
            return new Result(false,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            //删除失败
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }

    }

    //根据id查询checkitem表中数据，用于数据回显
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            //查询成功
           CheckItem checkItem = checkItemService.findById(id);
           return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        } catch (Exception e) {
            //查询失败
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

    //编辑数据存储
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            //编辑成功
            checkItemService.edit(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //执行出现异常表示操作失败，返回失败信息
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        //返回操作成功信息
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            //查询成功
            List<CheckItem> checkItemList = checkItemService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
        } catch (Exception e) {
            //查询失败
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }
}
